//
//  RegistViewController.m
//  hello mui
//
//  Created by zhangtao on 15/9/17.
//  Copyright (c) 2015年 zt.td. All rights reserved.
//

#import "RegistViewController.h"
#import "MainTabBarViewController.h"
#import "AppDelegate.h"
#import "LoginAndRegistViewController.h"
#import "AFNetworking.h"
@interface RegistViewController ()

@property (strong, nonatomic) IBOutlet UIView *phoneNumView;
@property (strong, nonatomic) IBOutlet UIView *psdView;
@property (strong, nonatomic) IBOutlet UIView *codeView;

@property (weak, nonatomic) IBOutlet UIButton *getYZM;
@property(nonatomic,assign)NSUInteger count;

@property (strong, nonatomic) IBOutlet UITextField *phoneNumText;
@property (strong, nonatomic) IBOutlet UITextField *psdText;
@property (strong, nonatomic) IBOutlet UITextField *codeText;

@property (strong, nonatomic) IBOutlet UIButton *submitBtn;





@property(nonatomic,strong)NSString *sessionid;
@property(nonatomic,strong)NSString *code;
@property(nonatomic,strong)NSString *codeTime;
@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.count = 59;
    _phoneNumView.layer.cornerRadius = 9;
    _psdView.layer.cornerRadius = 9;
    _codeView.layer.cornerRadius = 9;
    _getYZM.layer.cornerRadius = 9;
    _submitBtn.layer.cornerRadius = 9;

    _phoneNumText.placeholder = @"请输入手机号";
    _psdText.placeholder = @"请输入密码" ;
    _codeText.placeholder = @"输入验证码";
    _psdText.secureTextEntry = YES;
    
    _codeView.layer.borderColor = [RGB(131, 180, 240, 1)CGColor];
    _phoneNumView.layer.borderColor = _psdView.layer.borderColor  = rgb_box;
    _psdView.layer.borderWidth = _phoneNumView.layer.borderWidth = _codeView.layer.borderWidth = 1;
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)getYZM:(id)sender {
    if (_count>0 && _count<59) {
        //
    }else{
    
    NSString *path = @"login/getValidateCode";

    NSDictionary *dicss = @{@"mobile":_phoneNumText.text};
    
    NSDictionary *dicVal = @{@"mobile":_phoneNumText.text,@"bd_sig":[ToolObjc md5Str:dicss]};
    
    [[HttpConnctionManager sharedInstance]requestParam:dicVal andUrl:path andSuccessedBlock:^(NSDictionary *succeedResult) {
        _code = [succeedResult stringWithFilted:@"code"];
        _sessionid = [succeedResult stringWithFilted:@"sessionid"];
        NSString *recode = [succeedResult valueForKey:@"resCode"];
        _codeTime = [succeedResult valueForKey:@"codeTime"];
        NSLog(@"!!!~~~_code_%@,_session_%@,recode%@,codeTime_%@",_code,_sessionid,recode,_codeTime);
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];

        if ([[NSString stringWithFormat:@"%@",recode] isEqualToString:@"0"]) {   //get-->>success
            
            
            UIALERT(nil, @"验证码已发送至您的手机,请注意查收", nil, nil);
          
        }else if([[NSString stringWithFormat:@"%@",recode] isEqualToString:@"1"]){    //get--->faile-->>///
             UIALERT(nil, @"对不起,系统错误，请稍后重试", nil, nil);
           
        
           // *倒计时
//            if(59 == self.count)
//            {
//                //        [self.getYZM setBackgroundColor:RGB(200, 201, 202, 1)];
                [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
//            }

        }
    } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
        NSLog(@"error:::%@",error);
         UIALERT(nil, @"对不起,系统错误，请稍后重试", nil, nil);
    }];
    
    }
    
}
//59秒后才可再次获取验证码
- (void)countDown:(NSTimer *)timer
{
    [self.getYZM setTitle:[NSString stringWithFormat:@"%ld秒", (long)self.count] forState:UIControlStateNormal];
    self.count--;
    if(self.count <= 0)
    {

        [self.getYZM setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.getYZM setTitle:@" 重新获取" forState:UIControlStateNormal];
        [timer invalidate];
        self.count = 59;
    }
    
}
- (IBAction)backLogin:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}


- (IBAction)submit:(id)sender {
//    MainTabBarViewController *main = [self.storyboard instantiateViewControllerWithIdentifier:@"main"];
    if (_codeText.text.length !=0) {
        NSString *path = @"login/register";

//        NSMutableDictionary *dics = [NSMutableDictionary dictionaryWithObjectsAndKeys:_phoneNumText.text,@"mobile",_psdText.text,@"password",_code,@"code",_codeTime,@"codeTime", nil];
//        NSLog(@"dic::%@",dics);
        
        NSDictionary *dics = @{@"mobile":_phoneNumText.text,@"code":_codeText.text,@"codeTime":_codeTime,@"password":_psdText.text};
        NSDictionary *dicVal = @{@"mobile":_phoneNumText.text,@"code":_codeText.text,@"codeTime":_codeTime,@"password":_psdText.text,@"bd_sig":[ToolObjc md5Str:dics]};
        
        [[HttpConnctionManager sharedInstance]requestParam:dicVal andUrl:path andSuccessedBlock:^(NSDictionary *succeedResult) {
            NSLog(@"注册成功了吗？？%@",[succeedResult valueForKey:@"resMessage" ]);
            NSLog(@"%@",succeedResult);
            if ([[NSString stringWithFormat:@"%@",[succeedResult valueForKey:@"resCode"]] isEqualToString:@"0"]) {
                //--success
                [ToolObjc showMessage:@"注册成功"];
                
                NSUSER_DEF_NORSET(SELF_LOGIN, SHARE);
                           NSString *userid =[NSString stringWithFormat:@"%@", [[succeedResult valueForKey:@"resData"]valueForKey:@"userId"]];
                NSUSER_DEF_NORSET(userid, @"USERID");
                
                NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"address"], @"ADDRESS");
                
                NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"email"], @"EMAIL");
                
                NSString *str = [NSString stringWithFormat:@"%@", [[succeedResult valueForKey:@"resData"]stringWithFilted:@"gener"]];
                
                NSUSER_DEF_NORSET(str, @"GENER");
                
                NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"headImg"], @"HEADIMG");
                
                NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"nickName"], @"NICKNAME");
                
                NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"operatorId"], @"OPERATORID");
                
                NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"phone"], @"PHONE");
                
                NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"state"], @"STATE");
                
                
                NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"trueName"], @"TRUENAME");
                
                
                NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"userName"], @"USERNAME");
                
               
                LoginAndRegistViewController *login = [self.storyboard instantiateViewControllerWithIdentifier:@"loginAndRegist"];
//                MainTabBarViewController *main = [self.storyboard instantiateViewControllerWithIdentifier:@"main"];
                [self presentViewController:login animated:NO completion:nil];
            }else{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"注册失败，请稍后重试" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alert show];
            }
            
            
        } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
             UIALERT(nil, @"注册失败，请稍后重试", nil, nil);
        }];
        
        
    }else{
        UIALERT(nil, @"请正确填写信息", nil, nil);
    }
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_phoneNumText resignFirstResponder];
    [_psdText resignFirstResponder];
    [_codeText resignFirstResponder];
}
    
    
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
//    [manager POST:path parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"//register/%@",dic);
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        //
//    }];

    
    
    


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
